package com.personal.kousuan.generator;

import java.util.Random;

import com.personal.kousuan.domain.Question;
import com.personal.kousuan.factory.RandomGeneratorFactory;
import com.personal.kousuan.random.RandomGenerator;
import com.personal.kousuan.util.RandomUtil;

public abstract class PlusKouSuanGenerator implements KouSuanGenerator {

	

	@Override
	public Question generateKouSuanTi() {
		
		int i = 0;
		int j = 0;
		int one = 0;
		
		while(i == 0 || j == 9)
		{
			
		
			//随机产生第一个数
			one = RandomUtil.generateRandom(getMin(), getMax());
			
			//第一个数个位
			i = one % 10;
			//第一个数十位
			j = one / 10;
		}
		
		RandomGenerator randomGenerator = null;
		if(isJinWei()){
			randomGenerator = RandomGeneratorFactory.getInstance().getRandomGenerator(RandomGeneratorFactory.JINWEI_RANDOM_GENERATOR);
		}else{
			randomGenerator = RandomGeneratorFactory.getInstance().getRandomGenerator(RandomGeneratorFactory.BUJINWEI_RANDOM_GENERATOR);
		}
		
		
		//第二个数个位
		int k = randomGenerator.generateRandom(i,getGeWeiMax());
		
		randomGenerator = RandomGeneratorFactory.getInstance().getRandomGenerator(RandomGeneratorFactory.BUJINWEI_RANDOM_GENERATOR);
		//第二个数十位
		int l = randomGenerator.generateRandom(j,getShiWeiMax());
				
		int two = l * 10 + k;
		
		int three = 0;
		
		Question question = new Question();
		if(isContinuousCalculation()){
			int total = one + two;
			int temp = 99 - total > 0 ? 99 - total:0;
			if(temp == 0){
				three = 0;
			}else
			{
				
				try {
					three = RandomUtil.generateRandom(1, temp);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			question.setQuestion(one + "+" + two + "+" + three);
			question.setAnswer(String.valueOf(one + two + three));
		}else{
			question.setQuestion(one + "+" + two);
			question.setAnswer(String.valueOf(one + two));
		}
		
		
		
		return question;
	}
	public int getMin(){
		return 10;
	}
	public abstract int getMax();
	public abstract boolean isJinWei();
	public abstract boolean isContinuousCalculation();
	public abstract int getGeWeiMax();
	public abstract int getShiWeiMax();
	

}
